//app.js
let uploadFile = require('utils/uploadFile.js')//文件上传
App({
  uploadFile,
  onLaunch: function () {
    // 展示本地存储能力
    var logs = wx.getStorageSync('logs') || []
    logs.unshift(Date.now())
    wx.setStorageSync('logs', logs)

    // 登录
    wx.login({
      success: res => {
        // 发送 res.code 到后台换取 openId, sessionKey, unionId
      }
    })
    // 获取用户信息
    wx.getSetting({
      success: res => {
        if (res.authSetting['scope.userInfo']) {
          // 已经授权，可以直接调用 getUserInfo 获取头像昵称，不会弹框
          wx.getUserInfo({
            success: res => {
              // 可以将 res 发送给后台解码出 unionId
              this.globalData.userInfo = res.userInfo

              // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
              // 所以此处加入 callback 以防止这种情况
              if (this.userInfoReadyCallback) {
                this.userInfoReadyCallback(res)
              }
            }
          })
        }
      }
    })
  },
  globalData: {
    wasActivities: null,
    userInfo: null,
    // baseApi: 'https://' + '/user/',
    baseApi: 'https://devapi.xiaoshitimebank.com/user/sdx/'
    // baseApi: 'http://192.168.0.175:8087/'
  },


  getUserset () {
     if (wx.getStorageSync('token')!='') {
      wx.request({
        url: 'https://devapi.xiaoshitimebank.com/user/sdx/sdx/user/infos/auth',
        method: 'POST',
        header: {
          "content-type": "application/x-www-form-urlencoded; charset=UTF-8",
          "token": wx.getStorageSync('token'),
          "uid": wx.getStorageSync('uuid')
        },
        success: (res) => {
          if (res.data.code == 200) {
      
            // wx.removeStorageSync('token')
            return
          } else {
            wx.removeStorageSync('token')
      
            // wx.removeStorageSync('')
          }
        },
        fail: (error)=>{
          wx.removeStorageSync('token')
        }
      })
    }
  },
  request (url, params={}, method='POST') {
    var that = this
    var promise = new Promise((resolve, reject) => {
      wx.request({
        url: that.globalData.baseApi + url,
        data: params,
        method: method,
        header: method == 'POST' ?  {
          "content-type": "application/x-www-form-urlencoded; charset=UTF-8",
          "token": wx.getStorageSync('token'),
          "uid": wx.getStorageSync('uuid')
        } : {
          "Content-Type": "application/json",
          "token": wx.getStorageSync('token'),
          "uid": wx.getStorageSync('uuid')
        },
        complete: res => {
          if (res.data.success) {
            wx.stopPullDownRefresh()
            resolve(res.data)
          }else{
            reject(res)
          }
        }
      })
    })
    return promise
  },
  collectFormId(formid) {
    let that = this
    wx.request({
      url: that.globalData.baseApi + 'csq/msg/formId/collect/auth',
      method: 'post',
      header: {
        "content-type": "application/x-www-form-urlencoded; charset=UTF-8",
        "token": wx.getStorageSync('token'),
        "uid": wx.getStorageSync('uuid')
      },
      data: {formId: formid },
      complete: res => {
        if (res.data.success) {
          console.log('formid成功')
        }
      }
    })
  },

  onShow (options) {
    // if (wx.getStorageSync('token')!='') {
    //   wx.request({
    //     url: 'https://devapi.xiaoshitimebank.com/user/sdx/sdx/user/infos/auth',
    //     method: 'POST',
    //     header: {
    //       "content-type": "application/x-www-form-urlencoded; charset=UTF-8",
    //       "token": wx.getStorageSync('token'),
    //       "uid": wx.getStorageSync('uuid')
    //     },
    //     success: (res) => {
    //       if (res.data.code == 200) {
    //         wx.showToast({
    //           title: '11111111',
    //         })
    //         wx.removeStorageSync('token')
    //         // return
    //       } else {
    //        return
    //         // wx.removeStorageSync('')
    //       }
    //     },
    //     fail: (error)=>{
    //       wx.removeStorageSync('token')
    //     }
    //   })
    // }
    // var orderNo = options.referrerInfo.extraData.orderNo;


    // var success = options.referrerInfo.extraData.success;
},

  onLaunch (options) {
    // var orderNo = options.referrerInfo.extraData.orderNo;

    // var success = options.referrerInfo.extraData.success;

  },

  watch: function (ctx, obj) {
    Object.keys(obj).forEach(key => {
      this.observer(ctx.data, key, ctx.data[key], function (value) {
        obj[key].call(ctx, value)
      })
    })
  },
  // 监听属性，并执行监听函数
  observer: function (data, key, val, fn) {
    Object.defineProperty(data, key, {
      configurable: true,
      enumerable: true,
      get: function () {
        return val
      },
      set: function (newVal) {
        if (newVal === val) return
        fn && fn(newVal)
        val = newVal
      },
    })
  }

})
